package com.soilflow.web.controller.soilflow;


import com.soilflow.common.annotation.Log;
import com.soilflow.common.constant.UserConstants;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.page.TableDataInfo;
import com.soilflow.common.domain.dto.LandContractDto;
import com.soilflow.common.domain.vo.LandContractVo;
import com.soilflow.common.enums.BusinessType;
import com.soilflow.info.service.LandContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 合同Controller
 * 
 * @author relief
 * @date 2022-10-19
 */
@RestController
@RequestMapping("/land/contract")
public class ReliefContractController extends BaseController
{
    @Autowired
    private LandContractService landContractService;

    /**
     * 查询合同列表
     */
//    @PreAuthorize("@ss.hasPermi('land:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(LandContractDto landContract)
    {
        startPage();
        List<LandContractVo> list = landContractService.selectLandContractList(landContract);
        return getDataTable(list);
    }



    /**
     * 查询合同下拉列表
     */
    @GetMapping("/all")
    public AjaxResult listAll(LandContractDto landContract)
    {
        landContract.setPageSize(null);
        List<LandContractVo> list = landContractService.selectLandContractList(landContract);
        return AjaxResult.success(list);
    }

    /**
     * 获取合同详细信息
     */
//    @PreAuthorize("@ss.hasPermi('land:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getContract(@PathVariable("id") Long id)
    {
        return AjaxResult.success(landContractService.selectLandContractById(id));
    }

    /**
     * 新增合同
     */
//    @PreAuthorize("@ss.hasPermi('land:contract:add')")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LandContractDto landContract)
    {
//        if (UserConstants.NOT_UNIQUE.equals(landContractService.checkContractNameUnique(landContract.getContractName())))
//        {
//            return AjaxResult.error("新增合同'" + landContract.getContractName() + "'失败，合同名称已存在");
//        }
        return toAjax(landContractService.insertLandContract(landContract));
    }

    /**
     * 修改合同
     */
//    @PreAuthorize("@ss.hasPermi('land:contract:edit')")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LandContractDto landContract)
    {
        if (UserConstants.NOT_UNIQUE.equals(landContractService.checkContractNameUnique(landContract.getContractName())))
        {
            return AjaxResult.error("新增合同'" + landContract.getContractName() + "'失败，合同名称已存在");
        }
        return AjaxResult.success(landContractService.updateLandContract(landContract));
    }

    /**
     * 删除合同
     */
//    @PreAuthorize("@ss.hasPermi('land:contract:remove')")
    @Log(title = "合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable  List<Long> ids)
    {
        return toAjax(landContractService.removeByIds(ids));
    }
    /**
     * 查询今年合同数量
     */
    @GetMapping("/number")
    public AjaxResult number(){
        return AjaxResult.success(landContractService.number());
    }





}